/*
 * jxSwing Project (2012, 2013, 2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.text;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.text.Document;

import org.jxSwing.Cleanable;
import org.jxSwing.component.JXComponent;
import org.jxSwing.i18n.I18N;
import org.jxUtils.check.CheckUtil;

/**
 * @author Denilson Edinaldo Pinto
 * @project jxSwing
 * @package org.jxSwing.component.text
 * @date 14/09/2012 - 20:59:26
 */
public class TextField extends JTextField implements JXComponent, Cleanable {
	
	private static final long serialVersionUID = -6429163781243501830L;
	
	private boolean atributte = true;
	
	private boolean required = false;
	
	private boolean requiredBorderEnabled = true;
	
	private int index = -1;
	
	/**
	 *
	 */
	public TextField() {
	
		this(false);
	}
	
	/**
	 * @param required
	 */
	public TextField(final boolean required) {
	
		super();
		
		initGUI(required);
	}
	
	/**
	 * @param doc
	 * @param text
	 * @param columns
	 */
	public TextField(final Document doc, final String text, final int columns) {
	
		this(doc, text, columns, false);
	}
	
	/**
	 * @param doc
	 * @param text
	 * @param columns
	 * @param required
	 */
	public TextField(final Document doc, final String text, final int columns, final boolean required) {
	
		super(doc, text, columns);
		
		initGUI(required);
	}
	
	/**
	 * @param columns
	 */
	public TextField(final int columns) {
	
		this(columns, false);
	}
	
	/**
	 * @param columns
	 * @param required
	 */
	public TextField(final int columns, final boolean required) {
	
		super(columns);
		
		initGUI(required);
	}
	
	/**
	 * @param text
	 */
	public TextField(final String text) {
	
		this(text, false);
	}
	
	/**
	 * @param text
	 * @param required
	 */
	public TextField(final String text, final boolean required) {
	
		super(text);
		
		initGUI(required);
	}
	
	/**
	 * @param text
	 * @param columns
	 */
	public TextField(final String text, final int columns) {
	
		this(text, columns, false);
	}
	
	/**
	 * @param text
	 * @param columns
	 * @param required
	 */
	public TextField(final String text, final int columns, final boolean required) {
	
		super(text, columns);
		
		initGUI(required);
	}
	
	/**
	 * @param required
	 */
	private void initGUI(final boolean required) {
	
		setRequired(required);
		setSize(new Dimension(100, 25));
		setPreferredSize(new Dimension(100, 25));
	}
	
	/**
	 *
	 */
	@Override
	public void clear() {
	
		setText("");
	}
	
	@Override
	public int getIndex() {
	
		return index;
	}
	
	/**
	 * @return getText().toLowerCase()
	 */
	public String getLowerText() {
	
		return getText().toLowerCase();
	}
	
	/**
	 * @return getText().trim().toLowerCase()
	 */
	public String getTrimLowerText() {
	
		return getText().trim().toLowerCase();
	}
	
	/**
	 * @return getText().trim()
	 */
	public String getTrimText() {
	
		return getText().trim();
	}
	
	/**
	 * @return getText().trim().toUpperCase()
	 */
	public String getTrimUpperText() {
	
		return getText().trim().toUpperCase();
	}
	
	/**
	 * @return getText().toUpperCase()
	 */
	public String getUpperText() {
	
		return getText().toUpperCase();
	}
	
	@Override
	public boolean isAtributte() {
	
		return atributte;
	}
	
	@Override
	public boolean isRequired() {
	
		return required;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#isRequiredBorderEnabled()
	 */
	@Override
	public boolean isRequiredBorderEnabled() {
	
		return requiredBorderEnabled;
	}
	
	@Override
	public void setAtributte(final boolean atributte) {
	
		this.atributte = atributte;
	}
	
	@Override
	public void setIndex(final int index) {
	
		this.index = index;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Component#setName(java.lang.String)
	 */
	@Override
	public void setName(final String name) {
	
		super.setName(name);
		
		setRequired(isRequired());
	}
	
	@Override
	public void setRequired(final boolean required) {
	
		this.required = required;
		
		//
		if(required) {
			
			//
			if(isRequiredBorderEnabled()) {
				
				setBorder(new LineBorder(Color.RED));
			}
			
			//
			if(CheckUtil.isNullOrAbsoluteEmpty(getName())) {
				
				setToolTipText(I18N.component().requiredThis());
				
			}else {
				
				setToolTipText(I18N.component().requiredField(getName()));
			}
			
		}else {
			
			setBorder(UIManager.getBorder("TextField.border"));
			setToolTipText(null);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setRequiredBorderEnabled(boolean)
	 */
	@Override
	public void setRequiredBorderEnabled(final boolean requiredBorderEnabled) {
	
		this.requiredBorderEnabled = requiredBorderEnabled;
	}
}
